package com.css.apps.base.menu.action;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.css.apps.base.func.model.SFunc;
import com.css.apps.base.menu.model.SMenu;
import com.css.apps.base.role.model.SRole;
import com.css.apps.base.sys.model.SSys;
import com.css.core.model.tree.Tree;
import com.css.core.model.tree.TreeCache;
import com.css.core.model.tree.TreeNode;
import com.css.db.query.QueryCache;
import com.css.ids.server.service.dao.IdsFuncDao;
import com.css.ids.server.service.dao.IdsMenuDao;
import com.css.util.ListUtil;
import com.css.util.Messages;
import com.css.util.StringHelper;
import com.css.webbase.menu.MenuItem;
import com.css.webbase.webwork.action.UserAction;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@SuppressWarnings("serial")
public class PreviewMenu extends UserAction {

	private String type;
	private String id;
	
	@SuppressWarnings("unchecked")
	@Override
	protected String userGo() {
		if (StringHelper.isAnyEmpty(type, id)) {
			setMessage(Messages.getString("systemMsg.fieldEmpty"));
			return ERROR;
		}
		JSONArray res = new JSONArray();
		List<MenuItem> miList = null, menuList = new ArrayList<>();
		if ("role".equals(type)) {
			SRole role = QueryCache.get(SRole.class, id);
			miList = IdsMenuDao.listMenusByRole(role, role.getSysId());
			menuList.addAll(miList);
			if (ListUtil.isLstNotEmpty(miList)) {
				for (MenuItem mi : miList) {
					getSubMenuItem(mi, menuList);
				}
			}
			if (ListUtil.isLstNotEmpty(menuList)) {
				JSONObject m = null;
				for (MenuItem mi : menuList) {
					m = new JSONObject();
					m.put("id", mi.getId());
					m.put("name", mi.getName());
					m.put("pId", mi.getParentId());
					res.add(m);
				}
			}
		} else {
			List<SFunc> funcList = IdsFuncDao.getFuncByUserId(id);
			if (ListUtil.isLstNotEmpty(funcList)) {
				Set<String> sysIds = new HashSet<>(), funcIds = new HashSet<>();
				for (SFunc func : funcList) {
					sysIds.add(func.getSysId());
					funcIds.add(func.getFuncId());
				}
				JSONObject one = null;
				for (String sysId : sysIds) {
					SSys sys = QueryCache.get(SSys.class, sysId);
					if (sys != null) {
						one = new JSONObject();
						one.put("id", sys.getUuid());
						one.put("name", sys.getName());
						one.put("type", "sys");
						res.add(one);
					}
				}
				List<SMenu> rootMenus = new QueryCache("from SMenu where parentId in (:sysIds) and funcId in (:funcIdList)")
						.setParameter("sysIds", sysIds).setParameter("funcIdList", funcIds).list();
				Tree tree = new TreeCache(SMenu.class);
				for (SMenu rootMenu : rootMenus) {
					TreeNode root = tree.getTreeNode(rootMenu.getMenuId());
					one = new JSONObject();
					one.put("id", root.getNodeId());
					one.put("name", root.getName());
					one.put("pId", root.getParentId());
					one.put("type", "menu");
					one.put("isParent", true);
					res.add(one);
					List<TreeNode> children = root.getAllChildren();
					for (TreeNode child : children) {
						one = new JSONObject();
						one.put("id", child.getNodeId());
						one.put("name", child.getName());
						one.put("pId", child.getParentId());
						one.put("type", "menu");
						res.add(one);
					}
				}
			}
		}
		result = res.toString();
		return SUCCESS;
	}
	
	private void getSubMenuItem(MenuItem parent, List<MenuItem> menuList) {
		List<MenuItem> children = parent.getMenus();
		menuList.addAll(children);
		if (ListUtil.isLstNotEmpty(children)) {
			for (MenuItem child : children) {
				getSubMenuItem(child, menuList);
			}
		}
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}
